﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

using System;
using System.Text;
using System.IO;

using YTS.Log;

using IOFileServer.ZMPhoneticEnglish;
using IOFileServer.ZMPhoneticEnglish.Models;
using IOFileServer.ZMPhoneticEnglish.Implementation;

namespace IOFileServer.Test.ZMPhoneticEnglish
{
    [TestClass]
    public class TestIWordPronunciationCalcHandle
    {
        private ILog log;

        [TestInitialize]
        public void Init()
        {
            FileInfo fileInfo = new FileInfo($"./logs/TestIWordPronunciationCalcHandle/{DateTime.Now:yyyy_MM_dd}.log");
            log = new FilePrintLog(fileInfo, Encoding.UTF8);
        }

        [TestCleanup]
        public void Clean()
        {
        }

        [TestMethod]
        public void Test_WordPronunciationCalcHandle_Calc()
        {
            IWordPronunciationCalcHandle handle;
            string word;
            WordPronunciationCalcResult r;

            // 计算工具
            handle = new WordPronunciationCalcHandle();

            // 辅音: consonant    元音: vowel
            // 大字: large        小字: small

            #region 测试自编单词: cbulterlate
            // d = "0123456789";
            word = "cbulterlate";
            r = handle.Calc(word);
            Assert.IsNotNull(r);
            Assert.AreEqual(word, r.Word);
            Assert.AreEqual(word, r.WordAfterParsing);
            Assert.AreEqual(true, r.IsHaveEndEBridging);
            Assert.AreEqual(1, r.StressPosition);
            Assert.AreEqual(null, r.SecondaryRereadPosition);

            Assert.AreEqual(9, r.Rules.Length);
            AssertExtend.Check("consonant.large.c", "克", r.Rules, 0);
            AssertExtend.Check("consonant.large.b", "播", r.Rules, 1);
            AssertExtend.Check("vowel.large.u", "啊(口向上)", r.Rules, 2);
            AssertExtend.Check("consonant.large.l.double", "奥", r.Rules, 3);
            AssertExtend.Check("consonant.large.t", "特", r.Rules, 4);
            AssertExtend.Check("vowel.small.er", "耳", r.Rules, 5);
            AssertExtend.Check("consonant.large.l", "了", r.Rules, 6);
            AssertExtend.Check("vowel.large.a", "哎(大口)", r.Rules, 7);
            AssertExtend.Check("end.e", "", r.Rules, 8);

            Assert.AreEqual(10, r.CombinedPosition.Length);
            Assert.AreEqual(10, r.CombinedPositionAndRuleBind.Length);
            AssertExtend.Check_Combined(r, 0, "[0]", "[0]");
            AssertExtend.Check_Combined(r, 1, "[1]", "[1]");
            AssertExtend.Check_Combined(r, 2, "[2]", "[2]");
            AssertExtend.Check_Combined(r, 3, "[3]", "[3]");
            AssertExtend.Check_Combined(r, 4, "[4]", "[4]");
            AssertExtend.Check_Combined(r, 5, "[5,6]", "[5]");
            AssertExtend.Check_Combined(r, 6, "[7]", "[6]");
            AssertExtend.Check_Combined(r, 7, "[8]", "[7]");
            AssertExtend.Check_Combined(r, 8, "[9]", "[4]");
            AssertExtend.Check_Combined(r, 9, "[10]", "[8]");

            Assert.AreEqual(3, r.VowelPosition.Length);
            AssertExtend.Check("[2]", r.VowelPosition, 0);
            AssertExtend.Check("[5,6]", r.VowelPosition, 1);
            AssertExtend.Check("[8]", r.VowelPosition, 2);

            Assert.AreEqual(3, r.PinyinPosition.Length);
            AssertExtend.Check("[1,2]", r.PinyinPosition, 0);
            AssertExtend.Check("[4,6]", r.PinyinPosition, 1);
            AssertExtend.Check("[7,8]", r.PinyinPosition, 2);
            #endregion

            #region 单词: table
            // d = "01234";
            word = "table";
            r = handle.Calc(word);
            Assert.IsNotNull(r);
            Assert.AreEqual(word, r.Word);
            Assert.AreEqual(word, r.WordAfterParsing);
            Assert.AreEqual(false, r.IsHaveEndEBridging);
            Assert.AreEqual(0, r.StressPosition);
            Assert.AreEqual(null, r.SecondaryRereadPosition);

            Assert.AreEqual(6, r.Rules.Length);
            AssertExtend.Check("consonant.large.t", "特", r.Rules, 0);
            AssertExtend.Check("vowel.small.a", "A", r.Rules, 1);
            AssertExtend.Check("consonant.large.b", "播", r.Rules, 2);
            AssertExtend.Check("consonant.large.l.double", "奥", r.Rules, 3);
            AssertExtend.Check("end.e", "", r.Rules, 4);
            AssertExtend.Check("pinyin.l", "", r.Rules, 5);

            Assert.AreEqual(5, r.CombinedPosition.Length);
            Assert.AreEqual(5, r.CombinedPositionAndRuleBind.Length);
            AssertExtend.Check_Combined(r, 0, "[0]", "[0]");
            AssertExtend.Check_Combined(r, 1, "[1]", "[1]");
            AssertExtend.Check_Combined(r, 2, "[2]", "[2]");
            AssertExtend.Check_Combined(r, 3, "[3]", "[3]");
            AssertExtend.Check_Combined(r, 4, "[4]", "[4]");

            Assert.AreEqual(1, r.VowelPosition.Length);
            AssertExtend.Check("[1]", r.VowelPosition, 0);

            Assert.AreEqual(2, r.PinyinPosition.Length);
            AssertExtend.Check("[0,1]", r.PinyinPosition, 0);
            AssertExtend.Check("[2,3]", r.PinyinPosition, 1);
            #endregion
        }
    }
}
